let mainApp = {};
let request = function(
	url,
	data = {},
	method = "POST",
	type = 1,
	success,
	fail,
	complete
) {
	url = mainApp.config.globalProperties.baseUrl + url;
	let token = uni.getStorageSync('token') || "";
	var header = {
		token,
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			data,
			header,
			dataType: 'json',
			success: success,
			fail: (res) => {
				uni.showToast({
					title: res.errMsg,
					icon: 'none'
				})
				reject(res)
			},
			complete: (res) => {
				if (res.statusCode == 200) { //成功
					resolve(res.data)
					if (res.data.code && res.data.code != 200) {
						if (res.data.code == 900) {
							uni.showToast({
								title: '请稍后再试',
								icon: 'none'
							})
						} else if(res.data.code == '401') {
							if(type !== 2) {
								// uni.showToast({
								// 	title: '请先登录',
								// 	icon: 'none',
								// 	duration: 2000
								// })
								// console.log("测试", res);
								// setTimeout(function() {
								// 	uni.reLaunch({
								// 		url: '/pages/login/login'
								// 	})
								// }, 2000);
							}
						} else {
							uni.showToast({
								title: res.data.msg || res.data.message ||res.msg || res.message,
								icon: 'none',
								duration: 2000
							})
							reject(res);
						}

					}
				} else {
					uni.showToast({
						title: '请稍后再试',
						icon: 'none'
					})
					reject(res)
				}
			}
		})
	}).catch((e) => {});
};


export default function (app) {
	mainApp = app;
	app.config.globalProperties.$environment = 'dev'
	if (app.config.globalProperties.$environment === 'dev') {
		app.config.globalProperties.baseUrl = 'https://fish.cnyiji.cn/' //测试
	} else if(app.config.globalProperties.$environment === 'prod') {
		app.config.globalProperties.baseUrl = 'https://fish.cnyiji.cn/' //正式新
	}else{
		app.config.globalProperties.baseUrl = 'https://fish.cnyiji.cn/'
	}
	return request;
};
